/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlet;

import com.google.gson.Gson;
import frontend.MiddlewareHandler;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import middleware.Item;
import org.apache.thrift.TException;

/**
 *
 * @author chanhlt
 */
public class randomItemServlet extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse res) 
            throws IOException, ServletException 
    {
        res.setContentType("text/html; charset=utf-8");

        MiddlewareHandler handler = new MiddlewareHandler();
        Item item = null;
        try {
            item = handler.getRandomItem();
        } catch (TException ex) {
            Logger.getLogger(randomItemServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
        Gson gson=new Gson();
        String strItem=gson.toJson(item);
        res.getWriter().println(strItem);

    }
    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException{
        doGet(req, res);
    }
}
